package com.qms.repast.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.TextView;

import com.qms.repast.App;
import com.qms.repast.R;
import com.qms.repast.entity.resbean.LablesBean;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.SearchPresenter;
import com.qms.repast.ui.view.ISearchView;
import com.qms.repast.weidgets.common.ClearEditText;
import com.qms.repast.weidgets.common.LabelsView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/9/26 0020
 * @describe:
 */
public class SearchActivity extends BaseActivity<SearchPresenter> implements ISearchView {
	@BindView(R.id.iv_close)
	ImageView ivClose;
	@BindView(R.id.edt_clear)
	ClearEditText edtClear;
	@BindView(R.id.lbs_hots)
	LabelsView lbsHots;
	@BindView(R.id.iv_delete)
	ImageView ivDelete;
	@BindView(R.id.lbs_hostory)
	LabelsView lbsHostory;
	/**
	 * 热门标签
	 */
	private List<LablesBean> hotLables;

	private Intent mIntent;

	@Override
	protected int initLayout () {
		return R.layout.activity_search;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new SearchPresenter(this, this);
	}

	@Override
	protected void initView () {

	}

	@Override
	protected void initData () {
		hotLables = new ArrayList<>();
		hotLables.add(new LablesBean(1L, "0", "夫妻肺片"));
		hotLables.add(new LablesBean(2L, "0", "夫妻"));
		hotLables.add(new LablesBean(3L, "0", "夫妻肺片"));
		hotLables.add(new LablesBean(4L, "0", "肺片"));
		hotLables.add(new LablesBean(5L, "0", "夫妻肺片"));
		hotLables.add(new LablesBean(6L, "0", "魏家凉皮"));

		lbsHots.setLabels(hotLables, new LabelsView.LabelTextProvider<LablesBean>() {
			@Override
			public CharSequence getLabelText (TextView label, int position, LablesBean data) {
				return data.getLableName();
			}
		});

		lbsHostory.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
			@Override
			public void onLabelClick (TextView label, Object data, int position) {
				insertAndJump(label.getText().toString());
			}
		});

		edtClear.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction (TextView textView, int actionId, KeyEvent keyEvent) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					String keyWords = edtClear.getText().toString();
					if (!TextUtils.isEmpty(keyWords)) {
						insertAndJump(keyWords);
					}
					return true;
				}
				return false;
			}
		});
	}

	@Override
	protected void onResume () {
		super.onResume();
		mPresenter.selectHistory();
	}

	@OnClick({R.id.iv_close, R.id.iv_delete})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.iv_close:
				finish();
				break;
			case R.id.iv_delete:
				mPresenter.deleteHistory();
				break;
			default:
				break;
		}
	}

	/**
	 * 插入关键字并跳转
	 *
	 * @param keyWords
	 */
	public void insertAndJump (String keyWords) {
		LablesBean lablesBean = new LablesBean();
		lablesBean.setLableName(keyWords);
		lablesBean.setLableType("0");
		App.getmDaoSession().getLablesBeanDao().insertOrReplace(lablesBean);

		mIntent = new Intent();
		mIntent.setClass(SearchActivity.this, SearchResultActivity.class);
		mIntent.putExtra("keyWords", keyWords);
		startActivity(mIntent);
	}

	@Override
	public void updateHistory (List<LablesBean> beans) {
		if (null != beans) {
			lbsHostory.setLabels(beans, new LabelsView.LabelTextProvider<LablesBean>() {
				@Override
				public CharSequence getLabelText (TextView label, int position, LablesBean data) {
					return data.getLableName();
				}
			});
		}
	}
}
